<template>
  <div class="dashboard-view">
    <div class="page-header">
      <h1>数据仪表盘</h1>
      <div class="header-actions">
        <el-date-picker
          v-model="dateRange"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :shortcuts="dateShortcuts"
          value-format="YYYY-MM-DD"
        />
        <el-button type="primary" @click="refreshData">
          <el-icon><Refresh /></el-icon> 刷新
        </el-button>
      </div>
    </div>

    <!-- 数据概览卡片 -->
    <el-row :gutter="20" class="stat-cards">
      <el-col :span="6" v-for="(card, index) in statCards" :key="index">
        <el-card shadow="hover" :body-style="{ padding: '20px' }">
          <div class="stat-card">
            <div class="stat-icon" :style="{ backgroundColor: card.color }">
              <el-icon :size="24" color="#fff">
                <component :is="card.icon" />
              </el-icon>
            </div>
            <div class="stat-info">
              <div class="stat-value">{{ card.value }}</div>
              <div class="stat-title">{{ card.title }}</div>
              <div class="stat-trend" :class="card.trend > 0 ? 'up' : 'down'">
                {{ card.trend > 0 ? '+' : '' }}{{ card.trend }}% 较上期
              </div>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <!-- 图表区域 -->
    <el-row :gutter="20" class="chart-row">
      <el-col :span="16">
        <el-card class="chart-card">
          <template #header>
            <div class="card-header">
              <span>数据趋势</span>
              <el-radio-group v-model="trendType" size="small">
                <el-radio-button label="week">周</el-radio-button>
                <el-radio-button label="month">月</el-radio-button>
                <el-radio-button label="year">年</el-radio-button>
              </el-radio-group>
            </div>
          </template>
          <div class="chart-container" ref="trendChartRef"></div>
        </el-card>
      </el-col>
      <el-col :span="8">
        <el-card class="chart-card">
          <template #header>
            <div class="card-header">
              <span>数据分布</span>
            </div>
          </template>
          <div class="chart-container" ref="pieChartRef"></div>
        </el-card>
      </el-col>
    </el-row>

    <!-- 数据表格 -->
    <el-card class="data-table-card">
      <template #header>
        <div class="card-header">
          <span>最近数据记录</span>
          <el-button type="primary" link @click="viewAllData">查看全部</el-button>
        </div>
      </template>
      <el-table :data="recentData" style="width: 100%" border stripe>
        <el-table-column prop="id" label="ID" width="80" />
        <el-table-column prop="name" label="名称" min-width="120" />
        <el-table-column prop="category" label="分类" width="120" />
        <el-table-column prop="value" label="数值" width="100" />
        <el-table-column prop="date" label="日期" width="120" />
        <el-table-column prop="status" label="状态" width="100">
          <template #default="scope">
            <el-tag :type="scope.row.status === 'success' ? 'success' : 'info'">
              {{ scope.row.status === 'success' ? '成功' : '待处理' }}
            </el-tag>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, onBeforeUnmount, reactive } from 'vue'
import { useRouter } from 'vue-router'
import * as echarts from 'echarts'
import { 
  DataLine, 
  Histogram, 
  PieChart, 
  Refresh 
} from '@element-plus/icons-vue'

const router = useRouter()

// 日期范围选择
const dateRange = ref<[string, string]>(['2023-01-01', '2023-12-31'])

// 日期快捷选项
const dateShortcuts = [
  {
    text: '最近一周',
    value: () => {
      const end = new Date()
      const start = new Date()
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
      return [start, end]
    },
  },
  {
    text: '最近一个月',
    value: () => {
      const end = new Date()
      const start = new Date()
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
      return [start, end]
    },
  },
  {
    text: '最近三个月',
    value: () => {
      const end = new Date()
      const start = new Date()
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
      return [start, end]
    },
  },
]

// 趋势图类型
const trendType = ref('month')

// 统计卡片数据
const statCards = ref([
  {
    title: '总销售额',
    value: '¥ 126,560',
    icon: 'DataLine',
    color: '#409EFF',
    trend: 12.5
  },
  {
    title: '访问量',
    value: '88,546',
    icon: 'Histogram',
    color: '#67C23A',
    trend: 5.2
  },
  {
    title: '订单量',
    value: '6,521',
    icon: 'Document',
    color: '#E6A23C',
    trend: -2.5
  },
  {
    title: '转化率',
    value: '38%',
    icon: 'PieChart',
    color: '#F56C6C',
    trend: 3.8
  }
])

// 最近数据记录
const recentData = ref([
  { id: 1, name: '产品A销售数据', category: '销售', value: 2451, date: '2023-06-12', status: 'success' },
  { id: 2, name: '用户增长分析', category: '用户', value: 1892, date: '2023-06-11', status: 'pending' },
  { id: 3, name: '营销活动效果', category: '营销', value: 3854, date: '2023-06-10', status: 'success' },
  { id: 4, name: '客户满意度调查', category: '客户', value: 4.7, date: '2023-06-09', status: 'success' },
  { id: 5, name: '网站流量统计', category: '流量', value: 15628, date: '2023-06-08', status: 'pending' }
])

// 图表引用
const trendChartRef = ref<HTMLElement>()
const pieChartRef = ref<HTMLElement>()

// 图表实例
let trendChart: echarts.ECharts | null = null
let pieChart: echarts.ECharts | null = null

// 初始化趋势图
const initTrendChart = () => {
  if (!trendChartRef.value) return
  
  trendChart = echarts.init(trendChartRef.value)
  
  const option = {
    tooltip: {
      trigger: 'axis'
    },
    legend: {
      data: ['销售额', '访问量', '订单量']
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true
    },
    xAxis: {
      type: 'category',
      boundaryGap: false,
      data: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月']
    },
    yAxis: {
      type: 'value'
    },
    series: [
      {
        name: '销售额',
        type: 'line',
        stack: 'Total',
        smooth: true,
        data: [12000, 13200, 10100, 13400, 19000, 23200, 21100, 19300, 17400, 13200, 12500, 14800]
      },
      {
        name: '访问量',
        type: 'line',
        stack: 'Total',
        smooth: true,
        data: [2200, 1820, 1910, 2340, 2900, 3300, 3100, 2600, 2300, 1900, 1700, 1900]
      },
      {
        name: '订单量',
        type: 'line',
        stack: 'Total',
        smooth: true,
        data: [320, 332, 301, 334, 390, 430, 410, 394, 360, 320, 315, 355]
      }
    ]
  }
  
  trendChart.setOption(option)
}

// 初始化饼图
const initPieChart = () => {
  if (!pieChartRef.value) return
  
  pieChart = echarts.init(pieChartRef.value)
  
  const option = {
    tooltip: {
      trigger: 'item'
    },
    legend: {
      orient: 'vertical',
      left: 'left'
    },
    series: [
      {
        name: '数据分布',
        type: 'pie',
        radius: '70%',
        data: [
          { value: 1048, name: '类型A' },
          { value: 735, name: '类型B' },
          { value: 580, name: '类型C' },
          { value: 484, name: '类型D' },
          { value: 300, name: '类型E' }
        ],
        emphasis: {
          itemStyle: {
            shadowBlur: 10,
            shadowOffsetX: 0,
            shadowColor: 'rgba(0, 0, 0, 0.5)'
          }
        }
      }
    ]
  }
  
  pieChart.setOption(option)
}

// 刷新数据
const refreshData = () => {
  // 模拟刷新数据
  console.log('刷新数据', dateRange.value)
}

// 查看全部数据
const viewAllData = () => {
  router.push('/charts')
}

// 监听窗口大小变化
const handleResize = () => {
  trendChart?.resize()
  pieChart?.resize()
}

onMounted(() => {
  // 初始化图表
  initTrendChart()
  initPieChart()
  
  // 添加窗口大小变化监听
  window.addEventListener('resize', handleResize)
})

onBeforeUnmount(() => {
  // 移除窗口大小变化监听
  window.removeEventListener('resize', handleResize)
  
  // 销毁图表实例
  trendChart?.dispose()
  pieChart?.dispose()
})
</script>

<style scoped>
.dashboard-view {
  padding: 20px;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.page-header h1 {
  margin: 0;
  font-size: 24px;
  font-weight: 500;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 15px;
}

.stat-cards {
  margin-bottom: 20px;
}

.stat-card {
  display: flex;
  align-items: center;
}

.stat-icon {
  width: 48px;
  height: 48px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 15px;
}

.stat-info {
  flex: 1;
}

.stat-value {
  font-size: 24px;
  font-weight: bold;
  margin-bottom: 5px;
  color: #303133;
}

.stat-title {
  font-size: 14px;
  color: #909399;
  margin-bottom: 5px;
}

.stat-trend {
  font-size: 12px;
}

.stat-trend.up {
  color: #67C23A;
}

.stat-trend.down {
  color: #F56C6C;
}

.chart-row {
  margin-bottom: 20px;
}

.chart-card {
  margin-bottom: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.chart-container {
  height: 350px;
}

.data-table-card {
  margin-bottom: 20px;
}
</style> 